package com.hksj.Intelligentmanagement.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hksj.Intelligentmanagement.dto.UserRegionDTO;
import com.hksj.Intelligentmanagement.entity.UserRegion;
import com.hksj.Intelligentmanagement.mapper.UserRegionMapper;
import com.hksj.Intelligentmanagement.vo.UserRegionVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务类
 * @author chenxingqing
 */
@Service
public class UserRegionService extends ServiceImpl<UserRegionMapper, UserRegion> {

    public List<UserRegionVo> getRegionByUserId(String userId) {
        return baseMapper.getRegionByUserId(userId);
    }

    @Transactional
    public void saveUserRegion(UserRegionDTO dto) {
        lambdaUpdate().eq(UserRegion::getUserId,dto.getUserId()).remove();

        if (!ObjectUtils.isEmpty(dto.getRegionIds())){
            List<UserRegion> userRegions = new ArrayList<>();
            for (String regionId : dto.getRegionIds()) {
                UserRegion userRegion = new UserRegion();
                userRegion.setUserId(dto.getUserId());
                userRegion.setRegionId(regionId);
                userRegions.add(userRegion);
            }
            saveBatch(userRegions);
        }
    }
}
